package bdddaosimpl;

import java.util.List;

import bdd.beans.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class DAOJntexamenpromotionecImpl implements
		bdddaos.DAOJntexamenpromotionec {
	private DBLink dblink;

	public DAOJntexamenpromotionecImpl() {
		super();
		this.dblink = new DBLink();
	}

	@Override
	public Jntexamenpromotionec save(Jntexamenpromotionec o) {
		try {
			this.dblink.connexion();
			String s = "INSERT INTO JNTEXAMENPROMOTIONEC (NUMEXAMEN, NUMPROMOTION, NUMEC)" +
				" VALUES (" + o.getExamen().getNumexamen() +
                                ", " + o.getPromotion().getNumpromotion() +
				", " + o.getEc().getNumec() + ")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	@Override
	public Jntexamenpromotionec find(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Jntexamenpromotionec> findAll() {
		List<Jntexamenpromotionec> listeRetour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM JNTEXAMENPROMOTIONEC"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Jntexamenpromotionec retour = new Jntexamenpromotionec();
				retour.setExamen(new DAOExamenImpl().find(rs.getInt("NUMEXAMEN")));
				retour.setPromotion(new DAOPromotionImpl().find(rs.getInt("NUMPROMOTION")));
				retour.setEc(new DAOEcImpl().find(rs.getInt("NUMEC")));
				listeRetour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
		return listeRetour;
	}

	@Override
	public void update(Jntexamenpromotionec transientObject) {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(Jntexamenpromotionec o) {
		try {
			this.dblink.connexion();
			String s = "DELETE FROM JNTEXAMENPROMOTIONEC WHERE NUMEXAMEN=" + o.getExamen().getNumexamen() + " AND NUMPROMOTION =" + o.getPromotion().getNumpromotion() + " AND NUMEC =" + o.getEc().getNumec();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public Set<Jntexamenpromotionec> findByEc(Integer numEc) {
		Set<Jntexamenpromotionec> liste = new HashSet<Jntexamenpromotionec>();
		dblink.connexion();

		String query = "select * from jntexamenpromotionec where numec = "+numEc;

		try {
			PreparedStatement pst = dblink.getConnection().prepareStatement(query);
			ResultSet rs = pst.executeQuery();

			while(rs.next()) {
				Jntexamenpromotionec c = new Jntexamenpromotionec();

				c.setExamen(new DAOExamenImpl().find(rs.getInt("NUMEXAMEN")));
				c.setPromotion(new DAOPromotionImpl().find(rs.getInt("NUMPROMOTION")));
				c.setEc(new DAOEcImpl().find(rs.getInt("NUMEC")));
				liste.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			dblink.deconnexion();
		}
		return liste;
	}
        
    public Set<Jntexamenpromotionec> findByExamen(Integer numEtudiant) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Set<Jntexamenpromotionec> findByPromotion(Integer numPromotion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
